var original=""
var show=""
var ajaxPath="center.php"
window.onload=function()
{

 showInit()
 headShow()
 // 获取所有分类
    getAllCls()
    //在搜索框中按下特定键盘进行搜索
    keyDonwSearch()

 

 // addNumber()
 

 pandLogin()
}



//点击类别按钮在主页显示相应的标题
function toIndexShow(cls,e)
{
 more=document.getElementById("more")
 more.innerText="more"
 show=document.getElementById("show")
 getToEnter=document.getElementById("toEnter")
 var data={"flag":"getCls","cls":cls}
 ajax(data,ajaxPath,"POST",js=true,function(e)
  {
     window.scrollTo(0,0)
     data=JSON.parse(e)
     insert=""
     var dic={}
     for(i in data)
     {
         if(!Object.keys(dic).includes(data[i]["cls"]))
         {
             dic[data[i]["cls"]]=[data[i]]
         }
         else
         {
             dic[data[i]["cls"]].push(data[i])
         }
     }
     it=1

    
     for( k in dic[cls])
     {
            
         insert+="<p class='showTitle'>"+it+"、<a href='#'\
         cls="+cls+"&title="+dic[cls][k]["title"]+"  class='showTitle' \
         onclick=toRead('"+dic[cls][k]["cls"]+"','"+ dic[cls][k]["title"]+"')\
         >"+dic[cls][k]["title"]+"</a></p>"
         it+=1
      }
      show.innerHTML="<p class='showCls'>\
      <span>"+cls+"</span>\
      <span id='back'onclick=back() class='fa fa-mail-reply' aria-hidden='true'>\
      </span></p>"+insert
      getToEnter.setAttribute("href",getToEnter.getAttribute("href").split("?")[0]+"?cls="+cls);
  })
}


//点击标题后转到阅读页面
function toRead(cls,title)
{
    console.log(pandLogin(),cls)

 if(pandLogin()=="yes")
 {
  window.location.href="read.php?cls="+cls+"&title="+title
 }
 else
 {
  window.location.href="/"
 }
}

// 点击more后显示所以的分类
function showCls(e)
{
   closeOrOpen=e.innerText.replace(/\s*/ig,"")
   if(closeOrOpen=="more")
   {
    document.getElementById("cls").style.display="block"
    e.innerText="close"
   }
   else
   {
    document.getElementById("cls").style.display="none"
    e.innerText="more"
   }
}

//这里是模糊搜索，并按照类别显示
function searchKey()
{
 searchId=document.getElementById("searchValue")
 searchValue=searchId.value.replace(/\s*/ig,"")
 if (searchValue.length==0) 
 {
     window.location.reload(true)
  return
 }
 var dic={"flag":"findFile","searchValue":searchValue}
 ajax(dic,ajaxPath,"POST",js=true,function(e)
 {
  data=JSON.parse(e)
  insert=""
  dic={}
  for(index in data)
  {
   if( !Object.keys(dic).includes(data[index]["cls"]))
   {
    dic[data[index]["cls"]]=[data[index]["title"]]
   }
   else
   {
    dic[data[index]["cls"]].push(data[index]["title"])
   }
  }

  for( k in dic)
  {
   insert+="<div class='showArea' ><p class='showAreaCls'>\
   <span onclick=toIndexShow('"+k+"',this)>"+k+"</span>\
   <span id='back'  onclick=toIndexShow('"+k+"',this) class='fa fa-mail-reply' aria-hidden='true'>\
   </span></p>"
   for( v in dic[k])
   {
    insert+="<p class='showTitle'>\
    <a  onclick=toRead('"+k+"','"+dic[k][v]+"')>"+(v*1+1)+"、"+dic[k][v]+"</a></p>"
   }
   insert+="</div>"
  }
  document.getElementById("show").innerHTML=insert
  
 })
}
//点击具体分类之后返回







function back()
{
 var show=document.getElementById("show")
 show.innerHTML=original
}


//登录
function pandLogin()
{
   var usr=getCookie("usr")
   var passWord=getCookie("passWord")
   if(usr!="th"||passWord!="w2009")
   {
      window.location.href="/"
      return "no"
   }
  else
  {
   return "yes"
  }
  
}



/// 获取cookie
function getCookie(keyIndex)
{
    

    if(document.cookie.length==0)
    {
        return "no"
    }
    else
    {
        var cookies=document.cookie.split(";")
    }
    
    for(var i=0;i<cookies.length;i++)
    {
        var sp=cookies[i].split("=")
        var key=sp[0].trim()
        var value=sp[1].trim()
        if(keyIndex==key)
        {
            return value
        }
    }
    return "no"
    
}



// ajax函数
function ajax(dic,url,method,js=true,fun="")
{
 var xhr=new XMLHttpRequest()
 xhr.open(method,url,true)

  
  xhr.onreadystatechange = function(e)
 
 {
  
  if(xhr.readyState == 4&&xhr.status==200)
  {
   if(fun!="")
   {
    fun(xhr.response)
   }
  }
 }

 if(js)
 {
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(JSON.stringify(dic))
  
 }
 else
 {
  uploadPercent=0
  xhr.upload.addEventListener("progress",function(e)
  {
   uploadPercent=(e.loaded/e.total)*100
   showStatus("已经上传:"+uploadPercent+"%")
  })
  xhr.send(dic)
 }
}

//初始页面显示数据
function showInit()
{
 var data={"flag":"getInitData","count":10}
    ajax(data,"center.php","POST",js=true,function(e)
    {
        var data=JSON.parse(e)
        var show=document.getElementById("show")
        var insert=""
        var dic={}
        var row=""
        var insert=""
        var it=1
        for(i in data)
        {   
        
         row=data[i]
         if(!Object.keys(dic).includes(row["cls"]))
         {
          dic[row["cls"]]=[row["title"]]
         }
         else
         {
          dic[row["cls"]].push(row["title"])
         }
        }


        for( cls in dic)
     {
            insert+="<p class='showCls'>\
       <span>"+cls+"</span>\
       <span id='back'onclick=toIndexShow('"+cls+"',this) class='fa fa-mail-reply' aria-hidden='true'>\
       </span></p>"
       it=1
            for(k in dic[cls])
            {
             
             insert+="<p class='showTitle'>"+it+"、<a href='#'  class='showTitle' \
          onclick=toRead('"+cls+"','"+ dic[cls][k]+"')\
          >"+dic[cls][k]+"</span></a></p>"
          if(it==10)
          {
           break
          }
          it+=1
            }
         
      }
      show.innerHTML=insert
      original=insert

    })
}



// 获取所有分类
function getAllCls()
{   
    var data={"flag":"getAllCls"}
    var clsList=[]

    ajax(data,ajaxPath,"POST",js=true,function(e)
    {
        var data=JSON.parse(e)
        var clsBox=document.getElementById("cls")
        var row=0
        var insert=""
        for(k in data)
        {   
            row=data[k]
            cls=row["cls"]
            if(clsList.includes(cls))
            {
                continue
            }
            else
            {
                clsList.push(cls)
                insert+="<span class='moreCls' onclick=toTitleShow('"+cls+"',this)>"+cls+"</span>";
            }

        }
        clsBox.innerHTML=insert
        
    })
     
    
}


//点击导航后显示菜单
function toTitleShow(cls,e)
{
 toolflagtip=cls
 more=document.getElementById("more")
 more.innerText="more"
 readArea=document.getElementById("readArea")
 dic={"flag":"getCls","cls":cls}
 ajax(dic,ajaxPath,"POST",js=true,function(e)
 {
    window.scrollTo(0,0)
    data=JSON.parse(e)
    var show=document.getElementById('show')
    var clsBox=document.getElementById("cls")
    insert=""
    var dic={}

    for(i in data)
    {
        if(!Object.keys(dic).includes(data[i]["cls"]))
        {
            dic[data[i]["cls"]]=[data[i]]
        }
        else
        {
            dic[data[i]["cls"]].push(data[i])
        }
    }
    var it=0
    for( k in dic)
    {
        insert+="<div class='showArea' onclick=toIndexShow('"+k+"',this)>\
        <p class='showAreaCls'>\
        <span>"+k+"</span>\
        <span id='back'  onclick=back() class='fa fa-mail-reply' aria-hidden='true'></span></p>"
        for( v in dic[k])
        {
            insert+="<p class='showTitle'>"+(it+1)+"、"+"\
            <a  onclick=toRead('"+k+"','"+dic[k][v]["title"]+"')>"+dic[k][v]["title"]+"</a></p>"
         it+=1
        }
        
    }
    insert+="</div>"
    clsBox.style.display="none"
    show.innerHTML=insert

 })

}


//在搜索框中按下特定键盘进行搜索
function keyDonwSearch()
{   
    var searchValue=document.getElementById('searchValue')
    
    searchValue.onkeydown=function(e)
    {
        if("Enter"==e.code||e.code=="Space")
        {
            setTimeout("searchKey()",100)
        }
   }
}